#define _CRT_SECURE_NO_WARNINGS 1
#include<string>
#include<iostream>
using namespace std;


int myAtoi(string str) {
    size_t i = 0, x = 0;
    long more = 0;
    long less = 0;
    long num = 0;
    while (i < str.size())
    {
        if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')||str[i] == '.')
        {
            break;
        }
        for (i; (i < str.size() && ((str[i] >= '0' && str[i] <= '9') || ((str[i] == '-' || str[i] == '+') && ((i+1)<str.size()||(str[i + 1] >= '0' && str[i + 1] <= '9')||str[i+1] == '+'||str[i+1] == '-')))); i++)
        {
            if (str[i] == '-' || x == -1)
            {
                if (x == 0)
                {
                    i++;
                    x = -1;
                }
                if (str[i] == '+')
                {
                    i++;
                    less -= (str[i] - '0');
                    less *= 10;
                }
                else if (str[i] == '-')
                {
                    x = 1;
                }
                else
                {
                    less -= (str[i] - '0');
                    less *= 10;
                }
                if (less <= INT_MIN)
                {
                    less = INT_MIN;
                    break;
                }
            }
            else if (str[i] == '+' || x == 1)
            {
                if (x == 0)
                {
                    i++;
                    x = 1;
                }
                if (str[i] == '-')
                {
                    x = -1;
                }
                else if (str[i] == '+')
                {
                    i++;
                    more += str[i] - '0';
                    more *= 10;
                }
                else
                {
                    more += str[i] - '0';
                    more *= 10;
                }
                if (more >= INT_MAX)
                {
                    more = INT_MAX;
                    break;
                }
            }
            else
            {
                num += str[i] - '0';
                num *= 10;
            }
        }
        num = num + more + less;
        if (num != 0)
        {
            break;
        }
        i++;
    }
    if (num >= INT_MAX || num <= INT_MIN)
    {
        return num;
    }
    num /= 10;
    return num;
}


int main()
{
    string s1 = " +-2";
    int i = myAtoi(s1);
    //if (s1[0] <= '9' && s1[0] >= '0')
    //{
    //    cout << "sadasd" << endl;
    //}
    //int i = -40;
    //i -= 2;
    cout << i << endl;
}